package com.wiscamp.ninechapters.problems.domain.models.collections;

import com.wiscamp.ninechapters.ddd.AggregateRoot;
import lombok.Data;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 题集
 */
@Data
public class ProblemCollection implements AggregateRoot {
    @Serial
    private static final long serialVersionUID = 7868045644010279725L;

    /**
     * 题集编号
     */
    private long collectionId = 0;

    /**
     * 题集代码
     */
    private String collectionCode = "";

    /**
     * 题集名称
     */
    private String collectionName = "";

    /**
     * 兼容旧接口
     */
    private int collectionCategoryId = 0;

    /**
     * 考试编号
     */
    private long examId = 0;

    /**
     * 机构编号
     */
    private long organizationId = 0;

    /**
     * 语言代码
     */
    private String languageCode = "";

    /**
     * 学科编号
     */
    private long subjectId = 0;

    /**
     * 题数量
     */
    private int problemQuantity = 0;

    /**
     * 考试时长
     */
    private int totalTimeLimit = 0;

    /**
     * 考试总分数
     */
    private double totalScore = 0;

    /**
     * 是否真题
     */
    private boolean trueProblem = false;

    /**
     * 题集来源
     */
    private String collectionSource = "";

    /**
     * 题集摘要
     */
    private String collectionSummary = "";

    /**
     * 备注
     */
    private String remark = "";

    /**
     * 是否可用
     */
    private boolean enabled = true;

    /**
     * 是否逻辑删除
     */
    private boolean deleted = false;

    /**
     * 创建用户
     */
    private long creatorId = 0;

    /**
     * 创建时间
     */
    private LocalDateTime creationTime;

    /**
     * 编辑用户
     */
    private long modifierId = 0;

    /**
     * 编辑时间
     */
    private LocalDateTime modificationTime;

    private String examName = "";

    private String collectionCategoryName = "";

    private String creatorName = "";

    private String modifierName = "";

    /**
     * 题集中的题列表
     */
    private Collection<CollectionProblem> collectionProblems = new ArrayList<>();

    private Collection<CollectionPart> parts = new ArrayList<>();

    public void enable() {
        if (!this.enabled) {
            this.enabled = true;
        }
    }

    public void disable() {
        if (this.enabled) {
            this.enabled = false;
        }
    }

    public void delete() {
        if (!this.deleted) {
            this.deleted = true;
        }
    }

    public void createBy(long operatorId) {
        this.creatorId = operatorId;
        this.creationTime = LocalDateTime.now();
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public void updateBy(long operatorId) {
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public Collection<Long> getProblemIds() {
        return this.collectionProblems.stream().map(CollectionProblem::getProblemId).collect(Collectors.toList());
    }
}
